// An highlighted block
package com.xqy.wexmini.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xqy.wexmini.common.R;
import com.xqy.wexmini.domain.Province;
import com.xqy.wexmini.domain.Vegetable;
import com.xqy.wexmini.mapper.VegetableMapper;
import com.xqy.wexmini.service.ProvinceService;
import com.xqy.wexmini.utils.GouldUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "位置定位")
@RestController
public class PointController {

    @Autowired
    private GouldUtil gouldUtil;

    @Autowired
    private VegetableMapper vegetableMapper;
    @Autowired
    private ProvinceService provinceService;

    public static final Logger log = LoggerFactory.getLogger(PointController.class);

    //根据经纬度获取地址
    @ApiOperation("根据经纬度获取地址")
    @PostMapping("/getAddress")
    //@PreAuthorize("hasAuthority('普通成员')")
    public String getAddress(String longitude, String lat) {
        try {
            System.out.println(longitude);
            System.out.println(lat);
            String address = gouldUtil.getAMapByLngAndLat(longitude, lat);
            String[] strs=address.split("省");
            return  address;
        } catch (Exception e) {
            e.printStackTrace();
            return "获取失败";
        }
    }
    //根据地址获取经纬度
    @ApiOperation("根据地址获取经纬度")
    /*@GetMapping("/getLonLat/{address}")*/
    @PostMapping("/getLonLat")
    public String getLonLat(/*@PathVariable("address") */ String address) {
        try {
            String result = gouldUtil.getLonLat(address);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
            return  "获取失败";
        }
    }
    //通过地址算出两个地址之间的距离
    @ApiOperation("通过地址计算出两个地址之间的距离")
    @GetMapping("/getDistanceByAddress/{start}/{end}")
    public Long getDistanceByAddress(@PathVariable String start,@PathVariable String end){
        return gouldUtil.getDistanceByAddress(start,end);
    }

    @ApiOperation("（经、纬度）判断有哪些蔬菜")
    @PostMapping("/LatLongitudeVegetables")
    public List<Vegetable> getLatLongitudeVegetables(String longitude, String lat){
        String address=null;
        try {
            address = gouldUtil.getAMapByLngAndLat(longitude, lat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String str=getStr(address);

        System.out.println(str);
        QueryWrapper<Vegetable> wrapper=new QueryWrapper();
        wrapper.like("area",str);
        List<Vegetable> list = vegetableMapper.selectList(wrapper);
        //List<Vegetable> list = vegetableMapper.list1(strs[0]);
        return list;
    }
    @ApiOperation("（经、纬度）判断该地区的气候")
    @PostMapping("/LatLongitudeWeather")
    public R getLatLongitudeweather(String longitude, String lat){
        String address = null;
        Province one=null;
        try {
            address = gouldUtil.getAMapByLngAndLat(longitude, lat);
            String str=getStr(address);
            one = provinceService.getOne(new LambdaQueryWrapper<Province>().eq(Province::getCity, str));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok().put("province",one);
    }
    public String getStr(String address){
        String[] strshi=address.split("市");
        String[] strzi=address.split("自治区");
        String[] strs=address.split("省");
        String str=null;
        str=strs[0];
        System.out.println(str);
        if(strshi[0].equals("北京")||strshi[0].equals("重庆")||strshi[0].equals("上海")||strshi[0].equals("天津")){
            str=strshi[0];
        }
        if(strzi[0].equals("内蒙古")||strzi[0].equals("西藏")){
            str=strzi[0];
        }else if(strzi[0].equals("广西壮族")){
            str="广西";
        }else if(strzi[0].equals("新疆维吾尔")){
            str="新疆";
        }else if(strzi[0].equals("宁夏回族")){
            str="宁夏";
        }
        return str;
    }

}
